//package com.shuai.utils.log.aspect;
//
//import com.baomidou.mybatisplus.core.toolkit.SystemClock;
//import com.shuai.model.po.SysPermission;
//import com.shuai.model.po.SysRole;
//import com.shuai.service.SysPermissionService;
//import com.shuai.service.SysRoleService;
//import com.shuai.utils.log.annotation.SysLog;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.annotation.*;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.NamedThreadLocal;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.http.HttpServletRequest;
//import java.lang.reflect.Method;
//import java.util.Date;
//
///**
// *@Aspect 注解用来描述一个切面类，定义切面类的时候需要打上这个注解。
// * @Component 注解将该类交给 Spring 来管理
// */
//@Aspect  //表明该类是一个切面类，是切点和通知的集合
//@Component //把该类交给spring管理
//@Order(1)
//public class SysLogAspect {
//    private static final ThreadLocal<SysPermission> threadLog = new NamedThreadLocal<>("log");
//
//    @Autowired
//    private HttpServletRequest request;
//    @Autowired
//    private SysPermissionService sysRoleService;
//    /**
//     * 定义切点
//     */
//    @Pointcut("@annotation(com.shuai.utils.log.annotation.SysLog)")
//    public void pointAspect(){
//
//    }
//
//    /**
//     * 前置通知：方法执行之前处理
//     * @param joinPoint
//     */
//    @Before("pointAspect()")
//    public void deBefore(JoinPoint joinPoint){
//        //开始执行时间
//        Date beginTime = new Date();
//        //定义日志实体
//        SysPermission role = SysPermission.builder().code("123").build();
//
//        //保存日志
//        sysRoleService.addSysPermission(role);
//
//        threadLog.set(role);
//    }
//
//    /**
//     * 后置通知：方法执行之后处理
//     * @param joinPoint
//     */
//    @After("pointAspect()")
//    public void doAfter(JoinPoint joinPoint){
//        SysPermission role = threadLog.get();
//        Long endTime = SystemClock.now();
////        logEntity.setEndTime(DateUtils.parseDate(endTime));
//        //获取请求的方法
////        String name = joinPoint.getSignature().getName();
////        logEntity.setMethod(request.getMethod() + "-----"+name+"()");
////        logEntity.setRequestUri(request.getRequestURI());
//        //获取切入点所在的方法
//        MethodSignature  signature = (MethodSignature) joinPoint.getSignature();
//        Method method = signature.getMethod();
//        SysLog annotation = method.getAnnotation(SysLog.class);
//        if(annotation != null){
////            role.setTitle(annotation.value());
//            role.setType(1);
//        }
//        //获取请求的参数
//        Object[] args = joinPoint.getArgs();
//        int length = args.length;
////        if(length>0){
////            String jsonString = JsonToBeanUtils.toJsonString(args[0]);
////            //设置请求参数
////            logEntity.setParams(jsonString);
////        }
////        logEntity.setType("0");
//        //更新
//        sysRoleService.updateById(role);
//    }
//
////    /**
////     * 成功返回时的通知
////     * @param res
////     */
////    @AfterReturning(returning = "res",pointcut = "pointAspect()")
////    public void doReturn(Object res){
////        SysLogEntity logEntity = threadLog.get();
////        if(logEntity != null){
////            logEntity.setResultParams(JsonToBeanUtils.toJsonString(res));
////            //更新
////            sysLogTask.updateSysLog(logEntity);
//////            threadLog.set(logEntity);
////        }
////    }
//
//    /**
//     * 异常时的通知
//     * @param joinPoint
//     * @param e
//     */
//    @AfterThrowing(pointcut = "pointAspect()",throwing = "e")
//    public void doException(JoinPoint joinPoint,Throwable e){
//        SysPermission role = threadLog.get();
//        if(role != null){
//            role.setPath(e.toString());
//            //更新
//            sysRoleService.updateById(role);
//        }
//    }
//}
